/**
 * 抽奖
 */

//抽奖转盘效果
/*
 * 删除左右两端的空格
 */
function Trim(str){
	return str.replace(/(^\s*)|(\s*$)/g, ""); 
}
/*
 * 定义数组
 */
function GetSide(m,n){
    //初始化数组
    var arr = [];
    for(var i=0;i<m;i++){
        arr.push([]);
        for(var j=0;j<n;j++){
            arr[i][j]=i*n+j;
        }
    }
    //获取数组最外圈
    var resultArr=[];
    var tempX=0,
    tempY=0,
    direction="Along",
    count=0;
    while(tempX>=0 && tempX<n && tempY>=0 && tempY<m && count<m*n)
    {
        count++;
        resultArr.push([tempY,tempX]);
        if(direction=="Along"){
            if(tempX==n-1)
                tempY++;
            else
                tempX++;
            if(tempX==n-1&&tempY==m-1)
                direction="Inverse"
        }
        else{
            if(tempX==0)
                tempY--;
            else
                tempX--;
            if(tempX==0&&tempY==0)
                break;
        }
    }
    return resultArr;
}
   
var index=0,           //当前亮区位置
prevIndex=0,          //前一位置
Speed=300,           //初始速度
Time,           	 //定义对象
arr = GetSide(5,5),         //初始化数组
cycle=0,  		      //转动圈数   
EndCycle=0,          //计算圈数
flag=false,         //结束转动标志 
quick=0;           //加速
lottery_price = 2;
EndIndex=0;           //决定在哪一格变慢
lottery_info = ''; //中奖信息

function StartGame(){

	
	clearInterval(Time);
	cycle=0;
	flag=false;
	EndIndex = (lottery_price + 1 + Math.floor(Math.random()*16) )%16;
	if(EndIndex==0) EndIndex = 15;
	//EndIndex=Math.floor(Math.random()*16);
	//EndCycle=Math.floor(Math.random()*4);
	EndCycle=1;
	Time = setInterval(Star,Speed);
	return true;
}
   
function Star(num){
    //跑马灯变速
	if(flag==false){
		//走五格开始加速
		if(quick==5){
		    clearInterval(Time);
		    Speed=50;
		    Time=setInterval(Star,Speed);
		}
		//跑N圈减速
		if(cycle==EndCycle+1 && index==parseInt(EndIndex)){
		    clearInterval(Time);
		    Speed=300;
		    flag=true;       //触发结束			 
		    Time=setInterval(Star,Speed);
		}
    }
    
    if(index>=arr.length){
        index=0;
        cycle++;
    }
    
	//结束转动并选中号码
	//trim里改成数字就可以减速，变成Endindex的话就没有减速效果了
	if(flag==true && index==parseInt(lottery_price)-1){ 
		quick=0;
		clearInterval(Time);
	}
	$("#grid"+arr[index][0]+arr[index][1]).addClass('playcurr');
   	if(index>0)
    	prevIndex=index-1;
   	else{
     	prevIndex=arr.length-1;
   	}
  	$("#grid"+arr[prevIndex][0]+arr[prevIndex][1]).removeClass('playcurr');
	if(flag==true && index==parseInt(lottery_price)-1){ 
		lottery_checkprice(lottery_info);
	}
   	index++;
   	quick++;
}

//抽奖逻辑
function get_lottery_price() {
	if(!_checkLogin()) return false;
    $.ajax({
        type: "POST",
        url: _url+ '/Lotto/onLotto/',
        data: {
            isubmit: 1
        },
        dataType: 'JSON',
        success: function(data) {
            if (data.status == 1) {
            	lottery_price = data.location;
            	lottery_info = data;
            	StartGame();
            	
            	return true;
            	
            } else {
            	alert(data.msg);
                return false;
            }
        }
    });
}


function lottery_checkprice(data){
	alert(data.msg+"："+data.more);
	window.location.reload();
	return false;
}


//中奖信息
function lottery_checkprice_bac(data)
{
	if(data.data.item == 2 || data.data.item == 11 || data.data.item == 15){}	//积分奖品编号
	else if(data.data.item == 1 || data.data.item == 3 || data.data.item == 6 || data.data.item == 10 || data.data.item == 14){ //实物奖
		if(data.data.frame == '')
	    	Boxy.alert(data.data.info);
	    else
	    	Boxy.load(data.data.frame.url + new Date().getTime(), {title:data.data.info});
	}
	else if(data.data.item == 4 || data.data.item == 8 || data.data.item == 12 || data.data.item == 16){	//积分奖
		Boxy.alert('恭喜你抽中了2个积分，请查收');
		lotterynumtitle_c = lotterynumtitle_c + 2;
	}
	else if(data.data.item == 5){	//积分奖
		Boxy.alert('恭喜你抽中了5个积分，请查收');
		lotterynumtitle_c = lotterynumtitle_c + 5;
	}
	else if(data.data.item == 9){	//积分奖
		Boxy.alert('恭喜你抽中了10个积分，请查收');
		lotterynumtitle_c = lotterynumtitle_c + 10;
	}
	else if(data.data.item == 13){	//积分奖
		Boxy.alert('恭喜你抽中了15个积分，请查收');
		lotterynumtitle_c = lotterynumtitle_c + 15;
	}
	else if(data.data.item == 7){	//积分奖
		Boxy.alert('恭喜你抽中了20个积分，请查收');
		lotterynumtitle_c = lotterynumtitle_c + 20;
	}
	lotterynumtitle_n = Math.floor(lotterynumtitle_c/5);
	$('#lotterynumtitle').html('您还有'+lotterynumtitle_c+'积分可以再抽'+lotterynumtitle_n+'次');
}

//我要赚积分
function get_lottery_socre(){
	Boxy.load(_portalUrl + '/activity/index.php/Euro/First/lottery_socre/' + new Date().getTime(), {title:'我要赚积分'});
}

//奖品兑换
// JavaScript Document
/*$(function(){
	$('.euoboxy').boxy({modal:false,width:418,height:200});
	$('.hdboxy').boxy({modal:false,width:418,height:290});
});*/

function exchange(_id){
    if(!_checkLogin('noAjax')){
        var _url = _activityUrl + '/Euro/First/exchange/id/' + _id;
        window.top.Boxy.load(_url ,{title:'温馨提示',width:418,height:200});
        //window.location.href = _url;
        //var _elem = getFrame();
        //var _layUrl = _activityUrl + '/Euro/First/layerinfo';
        //window.top.Boxy.load(_layUrl ,{title:'填写个人信息',width:490,height:376});
        /* _elem.getJSON(_url, function(data){
            if(data.data == -1){}
        }); */
    }
}

function showProfile(){
    var _flag = $('#messageCode').html();
    if( _flag == -4){
        var _layUrl = _activityUrl + '/Euro/First/layerinfo';
        window.top.Boxy.load(_layUrl ,{title:'填写个人信息',width:490,height:376});
    }else{
        window.top.CloseWin();
    }
}


